var client = require("../DAOImpl/mongodb");
var dataConfig = require("../config/database.json");
var host = dataConfig.host;
var database = dataConfig.db;
var db = new client(host, database);
var status = require("../config/status.json");

exports.getNeareastDri=function getNeareastDri(driverspool,callback){
	console.log(driverspool);
	var result=new Object();
	var neareast=driverspool[0].distance;
	var duration=driverspool[0].duration;
	var dri_id=driverspool[0].dri_id;
	for (var index in driverspool){
		if(driverspool[index].distance < neareast){
			neareast=driverspool[index].distance;
			duration=driverspool[index].duration;
			dri_id=driverspool[index].dri_id;
		}else{
			
		}		
	}
	result.distance=neareast;
	result.duration=duration;
	
	var query = new Object();
	query.dri_id = dri_id;
	db.findOne(dataConfig.col_driver,query,function(err,dri_info){
		if(err){
			return callback(err);
		}else{
			result.dri_info = dri_info;
			return callback(err,result);
		}
		
	});
}

exports.findCondition = function findCondition(cus_lat, cus_lng){
	return  {
		"latitude": {
			"$gt": cus_lat - 50.0,
			"$lt": cus_lat + 50.0
		},
		"longitude": {
			"$gt": cus_lng - 50.0,
			"$lt": cus_lng + 50.0
		}
	};
}